//
//  PopPickerView.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/10.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit

class PopPickerView: UIView {

    typealias SendAreaStrBlock = (_ provinceId:String,_ cityId:String,_ townId:String) -> Void
    
    var tapSendAreaStr:SendAreaStrBlock?
    
    func callBackBlock(_ block: @escaping SendAreaStrBlock) {
        tapSendAreaStr = block
    }
    
    typealias SendAreaNameStrBlock = (_ provinceName:String,_ cityName:String,_ townName:String) -> Void
    
    var tapSendAreaNameStr:SendAreaNameStrBlock?
    
    func callNameBackBlock(_ block: @escaping SendAreaNameStrBlock) {
        tapSendAreaNameStr = block
    }
    
    var provinces = [AddressModel]()
    var citys = [AddressModel]()
    var towns = [AddressModel]()
    var provinceIdStr = "2"
    var cityIdStr = "2"
    var townIdStr = "36"
    
    var provinceNameStr = "北京市"
    var cityNameStr = "北京市"
    var townNameStr = "东城区"
    
    
    
    @IBOutlet var cancleBtn: UIButton!
    
    @IBOutlet var sureBtn: UIButton!
    
    @IBOutlet var chooseAreaPickerview: UIPickerView!
    
    class func getPopPickerView() -> PopPickerView {
        return UIView.creatFromNIB(By: "PopPickerView") as! PopPickerView
    }
    
    override func awakeFromNib() {
        self.frame = CGRect(x:0, y:kScreenHeight/2, width:kScreenWidth,height:ScaleHeight(380));
        configView()
    }
    
    func configView() -> Void {
        chooseAreaPickerview.dataSource = self
        chooseAreaPickerview.delegate = self
//        chooseAreaPickerview.selectRow(1,inComponent:0,animated:true)
//        chooseAreaPickerview.selectRow(2,inComponent:1,animated:true)
//        chooseAreaPickerview.selectRow(3,inComponent:2,animated:true)
    }
    

    
    
    @IBAction func cancleBtnPress(_ sender: UIButton) {
        if tapSendAreaStr != nil {
            self.tapSendAreaStr!("cancle", "cancle","cancle")
            }
    }
    
    @IBAction func sureBtnPress(_ sender: UIButton) {
        if tapSendAreaStr != nil {
            self.tapSendAreaStr!(provinceIdStr,cityIdStr,townIdStr)
        }
        if tapSendAreaNameStr != nil {
            self.tapSendAreaNameStr!(provinceNameStr,cityNameStr,townNameStr)
        }
    }
    
}

extension PopPickerView: UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return provinces.count
        case 1:
            return citys.count
        default:
            return towns.count
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            let model = provinces[row]
            return model.title
        case 1:
            let model = citys[row]
            return model.title
        default:
            let model = towns[row]
            return model.title
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            let model = provinces[row]
            self.citys = model.son!
            pickerView.reloadComponent(1)
            let cmodel = citys[0]
            self.towns = cmodel.son!
            let tmodel = self.towns[0]
            pickerView.reloadComponent(2)
            pickerView.selectRow(0, inComponent: 1, animated: true)
            provinceIdStr = model.id
            cityIdStr = cmodel.id
            townIdStr = tmodel.id
            provinceNameStr = model.title
            cityNameStr = cmodel.title
            townNameStr = tmodel.title
        case 1:
            let cmodel = citys[row]
            self.towns = cmodel.son!
            let tmodel = self.towns[0]
            pickerView.selectRow(0, inComponent: 2, animated: true)
            pickerView.reloadComponent(2)
            cityIdStr = cmodel.id
            townIdStr = tmodel.id
            cityNameStr = cmodel.title
            townNameStr = tmodel.title
        default:
            let tmodel = towns[row]
            townIdStr = tmodel.id
            townNameStr = tmodel.title
        }
        
    }
}
